
clear
set more off

cap log close
log using "$logdir/refund_by_size.log", replace

use $fromsasdir/result if ertc + ertc_amendment > 0, clear
drop if wg_amt/num_emp <= 20 | num_emp == 0 

gen refund = max(0, -balance_due)
gen y = min(1,(refund + ertc_amendment)/(ertc+ertc_amendment))



gen ertc_refund = min(ertc,refund)
gen ertc_f7200 = max(0, min(ertc-ertc_refund,f7200))
gen ertc_dep = max(0, ertc - ertc_refund - ertc_f7200)

foreach var in ertc_refund ertc_f7200 ertc_dep {
	qui replace `var' = `var'/(ertc+ertc_amendment)
}

gen post = tp >= 202103
gen w = ertc + ertc_amendment
collapse y num_emp ertc_refund ertc_f7200 ertc_dep (count) n = firm_id, by(post bin) fast

sum n
assert r(min) >= 5

*************************
*	Graph		*
*************************
qui replace num_emp = 3000 if bin == 3000
niceloglabels num_emp if num_emp > 0, style(13) local(labname)

local tstr0 "ERTC: 2020Q2-2020Q4"
local tstr1 "ERTC: 2021Q1-2021Q3"


twoway scatter y num_emp if num_emp > 0 & post == 0, mcolor(gs0) || ///
        scatter y num_emp if num_emp > 0 & post == 1, mcolor(gs8) msymbol(circle_hollow) ///
        xscale(log) xlabel(`labname') ///
			xtitle("Number of employees (log scale)") ///
			xline(500, lpattern("--") lcolor(gray)) ///
			ytitle("Share paid as refund or amended") ///
			scheme(s1color) ///
            legend(order(1 "2020Q2-2020Q4" 2 "2021Q1-2021Q4") cols(1))
 
graph export $outdir/refund_by_size.eps, replace
qui save $outdir/refund_by_size.dta, replace



twoway scatter ertc_f7200 num_emp if num_emp > 0 & post == 0, mcolor(gs0) || ///
        scatter ertc_f7200 num_emp if num_emp > 0 & post == 1, mcolor(gs8) msymbol(circle_hollow) ///
        xscale(log) xlabel(`labname') ///
			xtitle("Number of employees (log scale)") ///
			xline(500, lpattern("--") lcolor(gray)) ///
			ytitle("Share paid on Form 7200") ///
			scheme(s1color) ///
            legend(order(1 "2020Q2-2020Q4" 2 "2021Q1-2021Q4") cols(1))

list if bin <= 15
graph export $outdir/f7200_by_size.eps, replace

cap log close

